<!-- 
说明：表单
作者：刘永盛 
日期：2018-7-2 19:52:54 
版本：1.0.0
-->
<template>
  <el-dialog :title="title"
             append-to-body
             visible
             lock-scroll
             :before-close="handleClose"
             width="800px">
    <el-form ref="form"
             :model="form"
             :label-width="ui.formLabelWidth"
             :element-loading-text="loadingText"
             v-loading="busying">
      <el-row type="flex"
              :span="24">
        <el-col :span="12">
          <el-row>
            <mk-form-item label="文章标题"
                          prop="catitle"
                          :mk-le-length="50"
                          required>
              <el-input v-model="form.catitle"></el-input>
            </mk-form-item>
          </el-row>
          <el-row>
            <mk-form-item label="副标题"
                          prop="subtitle"
                          :mk-le-length="50"
                          required>
              <el-input v-model="form.subtitle"></el-input>
            </mk-form-item>
          </el-row>
        </el-col>

        <el-col :span="12">
          <mk-form-item label="文章主图"
                        prop="caimg"
                        :mk-le-length="300"
                        required>
            <mk-upload v-model="form.caimg"
                       :limit="1"
                       type="IMAGE"
                       :choose="true"
                       :mkTip="mkTip"
                       :width="210"
                       :height="99"
                       list-type="card"></mk-upload>
          </mk-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <mk-form-item label="文章描述"
                        prop="cadesc"
                        :mk-le-length="300"
                        required>
            <el-input v-model="form.cadesc"
                      type="textarea"
                      :autosize="{ minRows: 2, maxRows: 4}"></el-input>
          </mk-form-item>
        </el-col>
      </el-row>

      <el-row type="flex">
        <el-col :span="24">
          <mk-form-item label="文章内容"
                        prop="content"
                        required>
            <ueditor :token="token"
                     ref="ue"
                     style="margin-top:14px;"
                     v-model="form.content"
                     :compress="0.1"></ueditor>
          </mk-form-item>
        </el-col>
      </el-row>

    </el-form>
    <div slot="footer">
      <mk-button @click="handleClose">取消</mk-button>
      <mk-button @click="onSubmit"
                 :type="2">{{btnName}}</mk-button>
    </div>
  </el-dialog>
</template>

<script>
import { MkBase } from 'element-ui'
import formData from './form-data'
import MkUpload from 'sbin/upload'
import ueditor from 'element-ui/lib/mk-ueditor'
export default {
  name: 'cu-article-form',
  mixins: [MkBase],
  props: {
    psid: String,
    isAdd: Boolean,
    title: String,
    btnName: String,
    row: Object
  },
  components: {
    MkUpload,
    ueditor
  },
  async mounted () {
    // 获取upload.emeker.com 上传token
    this.token = this.$store.state.token
    if (this.isAdd === false) {
      await this.loadInfo()
    }
  },
  data () {
    const form = formData()
    return {
      mkTip: '只能上传.jpg/.jpeg/.png/.bmp文件,尺寸750*154px,且不能超过5M',
      ui: {
        formLabelWidth: '100px'
      },
      form,
      busying: false,
      loadingText: '正在保存',
      token: null
    }
  },
  methods: {
    async loadInfo () {
      let data = await this.$get('/cuArticle/view.json', {id: this.row.caid})
      this.form = data
      this.$refs.ue.setContent(this.form.content)
    },
    async valid () {
      if (!this.validate) {
        let fn = this.$refs.form.validate
        let nodejs = false
        this.validate = this.$promise({ fn, nodejs })
      }
      return await this.validate()
    },
    async onSubmit () {
      if (await this.valid()) {
        this.busying = true
        try {
          let data = await this[this.isAdd === false ? '$put' : '$post'](
            `/cuArticle/${this.isAdd === false ? 'update' : 'add'}.json`,
            {
              cuArticle: this.form
            }
          )
          if (data) {
            this.$emit('close', true)
          }
        } finally {
          this.busying = false
        }
      }
    },
    handleClose () {
      this.$emit('close')
    }
  }
}
</script>
